﻿/* File Created: 十一月 8, 2014 */
jQuery.jqUpload = {
    //主div，上传div，最大上传个数，最大上传大小，是否允许多传，文件类型（），上传单号，附件类别，上传成功执行函数，上传处理页面
    jUpload1: function (mainDom, upDom, upMaxlen, upMaxsize, isMulti, upFiletype, updanhao, upLb, okFun, uploadDopage,fileNameDom,rewriteHtml) {
        var mDom = $('#' + mainDom); var canUptype = "*.*"; var canTxt = "所有文件"; fileNameDom = fileNameDom != null ? fileNameDom : "up_files";
        rewriteHtml = rewriteHtml != null ? rewriteHtml : 1;
        if (rewriteHtml == 1) {
            mDom.html(stringFormat('<div id="{0}"></div><div class="up_buttons" style="display:none"><input type="button" class="noauto up_start" style="background-color:#13C5A5; border:none; color:White; padding:4px 8px; font-size:13px; text-align:center; "  value="开始上传"/> <input type="button"  class="noauto up_stop" style="background-color: red; border:none; color:White; padding:4px 8px; font-size:13px; text-align:center; "  value="取消上传"/></div><div><input type="text" id="{1}" name="{1}" style="width:360px"  readonly="readonly" /> </div>', upDom, fileNameDom));
        }
        switch (upFiletype) {
            case "doc":
                canUptype = "*.doc;*.docx"; canTxt = "word文档";
                break;
            case "xls":
                canUptype = "*.xls;*.xlsx"; canTxt = "excel文档";
                break;
            case "img":
                canUptype = "*.jpg;*.jpeg;*.png;*.cdr;*.dwg"; canTxt = "常用图片";
                break;
            case "office":
                canUptype = "*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx"; canTxt = "office文件";
                break;
            case "useful":
                canUptype = "*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.jpg;*.jpeg;*.png;*.cdr;*.dwg"; canTxt = "常用格式";
                break;
        }
        var uDom = mDom.find('#' + upDom); var buttonDom = mDom.find('.up_buttons'); var fileNameBox = mDom.find('#' + fileNameDom);
        var upDourl = '../Gupload/jupload/jupload.ashx';  
        if ($.browser.msie) { if ($.browser.version < 9.0 && (uploadDopage == null || uploadDopage.length < 8)) { upDourl = '../Gupload/jupload/jupload.ashx' } else if (uploadDopage.length > 8) { upDourl = uploadDopage } }
       
        uDom.uploadify({
            swf: '../Gupload/jupload/uploadify.swf',
            uploader: upDourl,
            height: 16,
            removeTimeout: 0,
            fileTypeDesc: canTxt,
            fileTypeExts: canUptype,
            fileSizeLimit: upMaxsize * 1024,
            formData: { 'danhao': updanhao, 'flb': upLb },
            queueSizeLimit: upMaxlen != null ? upMaxlen : 1, //队列中允许的最大文件数目
            progressData: 'all', // 'percentage''speed''all'//队列中显示文件上传进度的方式：all-上传速度+百分比，percentage-百分比，speed-上传速度
            removeCompleted: true, //上传成功后的文件，是否在队列中自动删除
            requeueErrors: true,
            multi: isMulti == 0 ? false : true,
            onDialogClose: function (swfuploadifyQueue) {//当文件选择对话框关闭时触发
                if (swfuploadifyQueue.filesQueued > 0) {
                    buttonDom.show();
                }
            },
            onUploadSuccess: function (file, data, response) {//上传完成时触发（每个文件触发一次）
                var oVal = fileNameBox.val();
                if (oVal.split(';').length < upMaxlen) {
                    fileNameBox.val(oVal + data.toString().split(',')[1] + ';');
                }
                else { fileNameBox.val(data.toString().split(',')[1] + ';'); }
            },
            onQueueComplete: function (stats) {//当队列中的所有文件全部完成上传时触发
                buttonDom.hide(); var oVal = fileNameBox.val(); fileNameBox.val(oVal.substring(0, oVal.length - 1));

                if (typeof (okFun) == 'function') { setTimeout(okFun, 200); }
            }
        });
        mDom.on('click', '.up_start', function () { $('.save_upfile').hide(); uDom.uploadify('upload', '*') }).on('click', '.up_stop', function () { uDom.uploadify('cancel', '*') });
    }


}